package com.lz.ui_lib.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

/**
 * 雷达动画
 * <p>
 * Created by Liuyk on 2015/10/23.
 */
public class RadarLayout extends RelativeLayout {

    //Layout的宽
    private int mWidth;
    //Layout的高
    private int mHeight;

    private Paint mPaint;
    private Paint mShadePaint;

    private int mDegrees;

    private final Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            mDegrees++;
            postDelayed(mRunnable, 50);
            invalidate();//重绘
        }
    };

    public RadarLayout(Context context) {
        this(context, null);
    }

    public RadarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mWidth = getWindowWidth(getContext());
        mHeight = getWindowHeight(getContext()) - getStatusHeight(getContext());

        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(3);
        mPaint.setStyle(Paint.Style.STROKE);

        mShadePaint = new Paint();
        mShadePaint.setColor(Color.parseColor("#fcfbf8"));
        mShadePaint.setStrokeWidth(0);
        mShadePaint.setStyle(Paint.Style.FILL);//需要实心
        final Shader shader = new SweepGradient(mWidth / 2, mHeight / 2, Color.parseColor("#fcfbf8"), Color.TRANSPARENT);
        mShadePaint.setShader(shader);//shader类完不成绘制阴影,我们去搜其子类

        post(mRunnable);//开始旋转
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void dispatchDraw(@NonNull Canvas canvas) {
        float radius = 0;
        float cx = mWidth / 2;
        float cy = mHeight / 2;
        float index = mWidth / 5;
        for (int i = 0; i < 4; i++) {
            radius = radius + index;//半径依次增大
            canvas.drawCircle(cx, cy, radius, mPaint);
        }
        canvas.save();
        canvas.rotate(mDegrees, cx, cy);
        canvas.drawCircle(cx, cy, radius, mShadePaint);//开始旋转阴影圆
        canvas.drawLine(cx, cy, cx + radius, cy, mPaint);
        canvas.restore();
        super.dispatchDraw(canvas);
    }

    public int getWindowWidth(Context ctx) {
        Display display = ((WindowManager) ctx
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);

        return metrics.widthPixels;
    }

    public int getWindowHeight(Context ctx) {

        Display display = ((WindowManager) ctx
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);

        return metrics.heightPixels;
    }

    public int getStatusHeight(Context context) {
        int statusHeight;
        final Rect localRect = new Rect();
        ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
        statusHeight = localRect.top;
        if (0 == statusHeight) {
            Class<?> localClass;
            try {
                localClass = Class.forName("com.android.internal.R$dimen");
                Object localObject = localClass.newInstance();
                int i5 = Integer.parseInt(localClass.getField("status_bar_height").
                        get(localObject).toString());
                statusHeight = context.getResources().getDimensionPixelSize(i5);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return statusHeight;
    }
}
